package cn.tedu.bonfire.front.content.service.impl;

import cn.tedu.bonfire.common.pojo.entity.Inform;
import cn.tedu.bonfire.front.content.dao.cache.ILikeCollectCacheRepository;
import cn.tedu.bonfire.front.content.dao.persist.repository.*;
import cn.tedu.bonfire.front.content.pojo.dto.UserLikeArticleDTO;
import cn.tedu.bonfire.front.content.pojo.dto.UserLikeCommentDTO;
import cn.tedu.bonfire.front.content.service.ILikeCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class LikeCollectServiceImpl implements ILikeCollectService {

    @Autowired
    private ILikeCollectRepository likeCollectRepository;

    @Autowired
    private ILikeCollectCacheRepository likeCollectCacheRepository;
    @Autowired
    private IArticleRepository articleRepository;

    @Autowired
    private IInformRepository informRepository;

    @Override
    public int setArticleLikeOrCollect(UserLikeArticleDTO likeArticleDTO) {
        Long userId = likeArticleDTO.getUserId();
        Long articleId = likeArticleDTO.getArticleId();
        Integer state = likeArticleDTO.getState();
        if (state == 1) {
            likeCollectRepository.updateLikeTimesByArticleId(articleId,0);
        }
        if (state == 2) {
            likeCollectRepository.updateCollectTimesByArticleId(articleId,0);
        }
        //插入点赞和收藏通知
        Inform inform = new Inform();
        inform.setInformId(userId);
        inform.setInformArticleId(articleId);
        if (state == 1){
            inform.setInformName("赞了你的文章");
        }
        if (state == 2) {
            inform.setInformName("收藏了你的文章");
        }
        inform.setInformType(2);
        inform.setIsNew(1);
        //插入被通知人id
        Long informUserId = articleRepository.getStandardById(articleId).getAuthorId();
        inform.setInformUserId(informUserId);
        inform.setCreateTime(new Date());
        inform.setUpdateTime(new Date());
        informRepository.insertInform(inform);

        Integer localStatus = likeCollectCacheRepository.getArticleStateByUserArticleInfo(userId, articleId);
        if (localStatus == 0) {
            return likeCollectCacheRepository.setArticleLikeOrCollect(likeArticleDTO);
        }
        if (localStatus != 0) {
            likeCollectCacheRepository.setArticleLikeOrCollectHaveStatus(likeArticleDTO);
        }
        return 0;
    }

    /**
     * 根据redis里数据状态决定取消点赞或收藏后的状态
     *
     * @param likeArticleDTO
     */
    @Override
    public void deleteLikeOrCollect(UserLikeArticleDTO likeArticleDTO) {
        Long userId = likeArticleDTO.getUserId();
        Long articleId = likeArticleDTO.getArticleId();
        Integer state = likeArticleDTO.getState();
        if (state == 1) {
            likeCollectRepository.updateLikeTimesByArticleId(articleId,1);
        }
        if (state == 2) {
            likeCollectRepository.updateCollectTimesByArticleId(articleId,1);
        }
        Integer localStatus = likeCollectCacheRepository.getArticleStateByUserArticleInfo(userId, articleId);
        likeArticleDTO.setState(0);
        if (localStatus == 3) {
            if (state == 1) {
                likeArticleDTO.setState(2);
            } else {
                likeArticleDTO.setState(1);
            }
        }
        likeCollectCacheRepository.deleteLikeOrCollect(likeArticleDTO);
    }
    /**
     * 查询笔记点赞收藏状态
     * @param userId
     * @param articleId
     * @return
     */
    @Override
    public Integer getArticleLikeCollection(Long userId, Long articleId) {
        Integer state = likeCollectCacheRepository.getArticleStateByUserArticleInfo(userId, articleId);
        return state;
    }


    /**
     * 根据登录用户id查询全部点赞笔记
     * @param userId
     * @return
     */
    @Override
    public Map<Long, Integer> getAllArticleLike(Long userId) {
        return likeCollectCacheRepository.getArticleStateByUser(userId);
    }

}

